package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.eclink.asset.domain.query.WarehouseQuery;
import com.eclink.asset.consume.domain.dao.ConsumeWarehouse;
import com.eclink.asset.consume.mapper.ConsumeWarehouseMapper;
import com.eclink.asset.service.NumberService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 仓库Service
 *
 * @author chenshixian
 */
@Service
public class ConsumeWarehouseService {

    @Resource
    private ConsumeWarehouseMapper consumeWarehouseMapper;

    @Resource
    private NumberService numberService;

    private static final int NUMBER_APPEND_LENGTH = 6;

    private String getZeroString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER_APPEND_LENGTH; i++) {
            sb.append("0");
        }
        return sb.toString();
    }

    private String getNumber() {

        String numberPrefix = "W";

        String lastNumber = numberService.getLastNumber("consume_warehouse", "warehouse_no", numberPrefix);
        int orderNo = 1;

        if (StringUtils.isNotBlank(lastNumber)) {

            int numberLength = lastNumber.length();
            orderNo = Integer.parseInt(lastNumber.substring(numberLength - NUMBER_APPEND_LENGTH, numberLength)) + 1;
        }
        String numberAppend = getZeroString() + orderNo;
        numberAppend = numberAppend.substring(numberAppend.length() - NUMBER_APPEND_LENGTH);

        return numberPrefix + numberAppend;
    }

    /**
     * 查询仓库
     *
     * @param id 仓库主键
     * @return 仓库
     */
    public ConsumeWarehouse selectById(Long id) {
        return consumeWarehouseMapper.selectById(id);
    }

    /**
     * 查询仓库名称
     *
     * @param id 仓库主键
     * @return 仓库
     */
    public String getWarehouseNameById(Long id) {
        ConsumeWarehouse warehouse = selectById(id);
        if (warehouse == null) {
            return null;
        }
        return warehouse.getWarehouseName();
    }

    /**
     * 查询列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 仓库
     */
    public List<ConsumeWarehouse> selectList(WarehouseQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ConsumeWarehouse> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String warehouseNo = query.getWarehouseNo();
        if (!StringUtils.isEmpty(warehouseNo)) {
            qw.eq("warehouse_no", warehouseNo);
        }
        String warehouseNameLike = query.getWarehouseNameLike();
        if (!StringUtils.isEmpty(warehouseNameLike)) {
            qw.like("warehouse_name", warehouseNameLike);
        }
        return consumeWarehouseMapper.selectList(qw);
    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<ConsumeWarehouse> selectListAll() {

        QueryWrapper<ConsumeWarehouse> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        return consumeWarehouseMapper.selectList(qw);
    }

    /**
     * 新增仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    public int insert(ConsumeWarehouse warehouse) {
        warehouse.setWarehouseNo(getNumber());
        warehouse.setDelFlag(0);
        warehouse.setCreateTime(LocalDateTime.now());
        return consumeWarehouseMapper.insert(warehouse);
    }

    /**
     * 修改仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    public int update(ConsumeWarehouse warehouse) {
        return consumeWarehouseMapper.updateById(warehouse);
    }

    /**
     * 批量删除仓库
     *
     * @param ids 需要删除的仓库主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        for(Long id : ids) {
            consumeWarehouseMapper.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除仓库信息
     *
     * @param id 仓库主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return consumeWarehouseMapper.deleteById(id);
    }

    /**
     * 根据主键集合查询仓库列表
     *
     * @param ids 主键集合
     * @return 仓库列表
     */
    public List<ConsumeWarehouse> selectByIdIn(Collection<Long> ids) {
        // 如果主键集合为空，直接返回空集合
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ConsumeWarehouse> qw = new QueryWrapper<>();
        qw.in("id",ids);
        return consumeWarehouseMapper.selectList(qw);
    }
}
